package net.stratusprogramming.school.Game.Framework.Scene;

import java.util.ArrayList;

import net.stratusprogramming.school.Game.Framework.System.InstanceMgr;
import net.stratusprogramming.school.Game.Framework.System.SaveMgr;

import org.newdawn.slick.Graphics;

/**
 * A collection of actors (and possibly of groups)
 * 
 * @author Lienau
 */
public class Group extends Actor {

	private Camera camera;
	protected ArrayList<Actor> children;
	
	/**
	 * Creates a new group
	 */
	public Group(Camera camera) {
		this.camera = camera;
		children = new ArrayList<Actor>();
	}
	
	/**
	 * Creates a new group
	 */
	public Group() {
		camera = InstanceMgr.camera;
		children = new ArrayList<Actor>();
	}
	
	/**
	 * Adds a new actor to the group
	 * 
	 * @param actor actor to add to the group 
	 */
	public void add(Actor actor) {
		actor.setParent(this);
		children.add(actor);
	}
	
	/**
	 * Adds a new actor to the group at specified index
	 * 
	 * @param index index to add actor at
	 * @param actor actor to add to the group 
	 */
	public void add(int index, Actor actor) {
		actor.setParent(this);
		children.add(index, actor);
	}
	
	/**
	 * Removes the actor at specified index
	 * 
	 * @param index index of actor to remove
	 * @return the actor that was removed
	 */
	public Actor remove(int index) {
		return children.remove(index);
	}
	
	/**
	 * Removes the actor from the group
	 * 
	 * @param actor actor to remove
	 * @return if the actor was removed
	 */
	public boolean remove(Actor actor) {
		return children.remove(actor);
	}
	
	@Override
	public void setParent(Group parent) {
		super.setParent(parent);
		this.camera = parent.camera;
	}

	@Override
	public void update(int delta) {
		for(int i = 0; i < children.size(); i++)
			children.get(i).update(delta);		
	}

	@Override
	public void render(Graphics g) {
		for(int i = 0; i < children.size(); i++)
			camera.render(children.get(i), g);	
	}

	@Override
	public void save(SaveMgr saveMgr) {
		for(int i = 0; i < children.size(); i++)
			children.get(i).save(saveMgr);
		
	}

	@Override
	public void load(SaveMgr saveMgr) {
		for(int i = 0; i < children.size(); i++)
			children.get(i).load(saveMgr);		
	}

}
